/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import biz.AccountBO;
import biz.UserDetailBO;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Young Ran
 */
public class PanelRegisterApprove extends javax.swing.JPanel {

    public static final int APPROVE_MODE = 0;
    public static final int PROMOTE_MODE = 1;
    private MTableModel model;
    private int mode;

    /**
     * Creates new form PanelRegisterApprove
     */
    public PanelRegisterApprove(int mode) {
        initComponents();
        try {
            switch (mode) {
                case APPROVE_MODE: {
                    btnAdmin.setVisible(false);
                    btnCoach.setVisible(false);
                    btnUser.setVisible(false);
                    model = new MTableModel(new UserDetailBO().getAllUnapprovedUserDetail(), MTableModel.ACC_APP_MODE);

                    tblDisplay.setModel(model);
                }
                break;
                case PROMOTE_MODE: {
                    btnApprove.setVisible(false);
                    model = new MTableModel(new UserDetailBO().getAllUserDetail(), MTableModel.ACC_APP_MODE);
                    tblDisplay.setModel(model);
                }
                break;
            }
        } catch (SQLException ex) {
            Logger.getLogger(PanelRegisterApprove.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void promote(int level) {
        int i = JOptionPane.showConfirmDialog(this, "Do you want to Approve this?", "Comfirm Dialog ", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (i == 0) // neu nguoi dung chon "Yes"
        {
            try {
                String value = (String) model.getValueAt(tblDisplay.getSelectedRow(), 6);
                new AccountBO().promoteAccount(value, level);
                JOptionPane.showMessageDialog(this, "Approve successful", "Message Dialog Demo", JOptionPane.INFORMATION_MESSAGE);
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(this, "Database Connection Error", "Runtime Error", JOptionPane.ERROR_MESSAGE);
            }
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlRegisterApprove = new javax.swing.JPanel();
        scrlDisplay = new javax.swing.JScrollPane();
        tblDisplay = new javax.swing.JTable();
        pnlControlBtn = new javax.swing.JPanel();
        btnApprove = new javax.swing.JButton();
        btnAdmin = new javax.swing.JButton();
        btnCoach = new javax.swing.JButton();
        btnUser = new javax.swing.JButton();

        setBackground(new java.awt.Color(184, 192, 13));

        pnlRegisterApprove.setBackground(new java.awt.Color(184, 192, 13));

        tblDisplay.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Id", "Username", "Name", "Age", "Sex", "Phone", "Email", "Username"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Boolean.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        scrlDisplay.setViewportView(tblDisplay);

        javax.swing.GroupLayout pnlRegisterApproveLayout = new javax.swing.GroupLayout(pnlRegisterApprove);
        pnlRegisterApprove.setLayout(pnlRegisterApproveLayout);
        pnlRegisterApproveLayout.setHorizontalGroup(
            pnlRegisterApproveLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrlDisplay, javax.swing.GroupLayout.DEFAULT_SIZE, 787, Short.MAX_VALUE)
        );
        pnlRegisterApproveLayout.setVerticalGroup(
            pnlRegisterApproveLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrlDisplay, javax.swing.GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE)
        );

        pnlControlBtn.setBackground(new java.awt.Color(225, 156, 24));

        btnApprove.setBackground(new java.awt.Color(0, 0, 0));
        btnApprove.setForeground(new java.awt.Color(255, 255, 255));
        btnApprove.setText("Approve");
        btnApprove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnApproveActionPerformed(evt);
            }
        });
        pnlControlBtn.add(btnApprove);

        btnAdmin.setBackground(new java.awt.Color(0, 0, 0));
        btnAdmin.setForeground(new java.awt.Color(255, 255, 255));
        btnAdmin.setText("Admin");
        btnAdmin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAdminActionPerformed(evt);
            }
        });
        pnlControlBtn.add(btnAdmin);

        btnCoach.setBackground(new java.awt.Color(0, 0, 0));
        btnCoach.setForeground(new java.awt.Color(255, 255, 255));
        btnCoach.setText("Coach");
        btnCoach.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCoachActionPerformed(evt);
            }
        });
        pnlControlBtn.add(btnCoach);

        btnUser.setBackground(new java.awt.Color(0, 0, 0));
        btnUser.setForeground(new java.awt.Color(255, 255, 255));
        btnUser.setText("User");
        btnUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUserActionPerformed(evt);
            }
        });
        pnlControlBtn.add(btnUser);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlControlBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(pnlRegisterApprove, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(1, 1, 1))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(pnlRegisterApprove, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlControlBtn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnApproveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnApproveActionPerformed
        try {
            // TODO add your handling code here:
            int i = JOptionPane.showConfirmDialog(this, "Do you want to Approve this?", "Comfirm Dialog ", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (i == 0) // neu nguoi dung chon "Yes"
            {
                int index = (Integer) model.getValueAt(tblDisplay.getSelectedRow(), 0);
                new UserDetailBO().approveUser(index);
                model.removeRow(tblDisplay.getSelectedRow());
                model.fireTableDataChanged();
                JOptionPane.showMessageDialog(this, "Approve successful", "Message Dialog Demo", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Error: Database Link Error", "Message Dialog", JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_btnApproveActionPerformed

    private void btnAdminActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAdminActionPerformed
        // TODO add your handling code here:
        promote(2);
    }//GEN-LAST:event_btnAdminActionPerformed

    private void btnCoachActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCoachActionPerformed
        // TODO add your handling code here:
        promote(1);
    }//GEN-LAST:event_btnCoachActionPerformed

    private void btnUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUserActionPerformed
        // TODO add your handling code here:
        promote(0);
    }//GEN-LAST:event_btnUserActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdmin;
    private javax.swing.JButton btnApprove;
    private javax.swing.JButton btnCoach;
    private javax.swing.JButton btnUser;
    private javax.swing.JPanel pnlControlBtn;
    private javax.swing.JPanel pnlRegisterApprove;
    private javax.swing.JScrollPane scrlDisplay;
    private javax.swing.JTable tblDisplay;
    // End of variables declaration//GEN-END:variables
}
